const mongoose = require('./db/setmongoose');
const bkcModel = require('./db/bkc');

// 等待数据库连接
setTimeout(async () => {
  try {
    console.log('开始初始化订单数据...');
    
    // 检查是否已有订单数据
    const existingOrders = await bkcModel.countDocuments({ orderId: { $exists: true } });
    
    if (existingOrders > 0) {
      console.log(`数据库中已有 ${existingOrders} 条订单数据，无需重复初始化`);
      return;
    }
    
    // 完整的10条订单数据
    const ordersData = [
      {
        orderId: 'ORD001',
        orderType: '代取快递',
        orderStatus: '待付款',
        orderTitle: '顺丰快递代取',
        orderDescription: '顺丰快递，单号：SF1234567890，取件码：1234',
        orderAmount: 5.00,
        orderAddress: '菜鸟驿站（图书馆对面）',
        orderPhone: '13800138000',
        orderTime: new Date('2025-01-15 10:30:00'),
        customerId: 'CUST001',
        customerName: '张三',
        paymentMethod: '微信',
        paymentStatus: '未支付',
        remark: '请尽快取件，谢谢',
        tags: ['快递', '顺丰']
      },
      {
        orderId: 'ORD002',
        orderType: '代取外卖',
        orderStatus: '待接单',
        orderTitle: '美团外卖代取',
        orderDescription: '美团外卖，订单号：MT987654321，商家：黄焖鸡米饭',
        orderAmount: 8.00,
        orderAddress: '美团外卖柜（宿舍楼下）',
        orderPhone: '13800138001',
        orderTime: new Date('2025-01-15 11:15:00'),
        customerId: 'CUST002',
        customerName: '李四',
        paymentMethod: '支付宝',
        paymentStatus: '已支付',
        remark: '请小心取餐，不要洒了',
        tags: ['外卖', '美团']
      },
      {
        orderId: 'ORD003',
        orderType: '代取外卖',
        orderStatus: '待取货',
        orderTitle: '饿了么外卖代取',
        orderDescription: '饿了么外卖，订单号：EL555666777，商家：麻辣烫',
        orderAmount: 6.00,
        orderAddress: '饿了么外卖柜（教学楼门口）',
        orderPhone: '13800138002',
        orderTime: new Date('2025-01-15 12:00:00'),
        customerId: 'CUST003',
        customerName: '王六',
        paymentMethod: '微信',
        paymentStatus: '已支付',
        remark: '请尽快送达',
        tags: ['外卖', '饿了么']
      },
      {
        orderId: 'ORD004',
        orderType: '代取快递',
        orderStatus: '待送达',
        orderTitle: '圆通快递代取',
        orderDescription: '圆通快递，单号：YT111222333，取件码：5678',
        orderAmount: 4.50,
        orderAddress: '菜鸟驿站（食堂旁边）',
        orderPhone: '13800138003',
        orderTime: new Date('2025-01-15 09:00:00'),
        customerId: 'CUST004',
        customerName: '钱八',
        paymentMethod: '余额',
        paymentStatus: '已支付',
        remark: '请送到宿舍楼下',
        tags: ['快递', '圆通']
      },
      {
        orderId: 'ORD005',
        orderType: '代取外卖',
        orderStatus: '已送达',
        orderTitle: '肯德基外卖代取',
        orderDescription: '肯德基外卖，订单号：KFC888999000，套餐：香辣鸡腿堡套餐',
        orderAmount: 12.00,
        orderAddress: '肯德基外卖柜（校门口）',
        orderPhone: '13800138004',
        orderTime: new Date('2025-01-15 08:00:00'),
        customerId: 'CUST005',
        customerName: '周十',
        paymentMethod: '支付宝',
        paymentStatus: '已支付',
        remark: '请保持温度',
        tags: ['外卖', '肯德基']
      },
      {
        orderId: 'ORD006',
        orderType: '闲置交易',
        orderStatus: '已完成',
        orderTitle: '二手自行车交易',
        orderDescription: '九成新自行车，品牌：捷安特，价格：200元',
        orderAmount: 200.00,
        orderAddress: '宿舍楼下',
        orderPhone: '13800138005',
        orderTime: new Date('2025-01-14 15:00:00'),
        customerId: 'CUST006',
        customerName: '郑十二',
        paymentMethod: '微信',
        paymentStatus: '已支付',
        remark: '车况很好，很满意',
        tags: ['闲置', '自行车']
      },
      {
        orderId: 'ORD007',
        orderType: '宿舍小店',
        orderStatus: '已完成',
        orderTitle: '零食购买',
        orderDescription: '薯片、可乐、巧克力等零食，总价：25元',
        orderAmount: 25.00,
        orderAddress: '3号宿舍楼',
        orderPhone: '13800138006',
        orderTime: new Date('2025-01-14 20:00:00'),
        customerId: 'CUST007',
        customerName: '李十四',
        paymentMethod: '微信',
        paymentStatus: '已支付',
        remark: '零食很新鲜',
        tags: ['零食', '宿舍']
      },
      {
        orderId: 'ORD008',
        orderType: '代取须知',
        orderStatus: '已过期',
        orderTitle: '图书馆书籍代取',
        orderDescription: '《高等数学》教材，图书馆借阅，已过期',
        orderAmount: 3.00,
        orderAddress: '图书馆借阅处',
        orderPhone: '13800138007',
        orderTime: new Date('2025-01-10 14:00:00'),
        customerId: 'CUST008',
        customerName: '张十五',
        paymentMethod: '微信',
        paymentStatus: '已支付',
        remark: '请尽快处理',
        tags: ['书籍', '图书馆']
      },
      {
        orderId: 'ORD009',
        orderType: '骑手认证',
        orderStatus: '已完成',
        orderTitle: '骑手身份认证',
        orderDescription: '完成骑手身份认证，获得接单权限',
        orderAmount: 0.00,
        orderAddress: '线上认证',
        orderPhone: '13800138008',
        orderTime: new Date('2025-01-13 10:00:00'),
        customerId: 'CUST009',
        customerName: '陈十七',
        paymentMethod: '免费',
        paymentStatus: '已支付',
        remark: '认证成功',
        tags: ['认证', '骑手']
      },
      {
        orderId: 'ORD010',
        orderType: '代取快递',
        orderStatus: '待付款',
        orderTitle: '京东快递代取',
        orderDescription: '京东快递，单号：JD444555666，取件码：9999',
        orderAmount: 6.50,
        orderAddress: '京东快递柜（体育馆旁边）',
        orderPhone: '13800138009',
        orderTime: new Date('2025-01-15 13:00:00'),
        customerId: 'CUST010',
        customerName: '杨十八',
        paymentMethod: '支付宝',
        paymentStatus: '未支付',
        remark: '请尽快取件',
        tags: ['快递', '京东']
      }
    ];
    
    // 插入订单数据
    const result = await bkcModel.insertMany(ordersData);
    
    console.log(`✅ 成功初始化 ${result.length} 条订单数据！`);
    console.log('订单ID列表:');
    result.forEach(order => {
      console.log(`- ${order.orderId}: ${order.orderTitle} (${order.orderStatus})`);
    });
    
  } catch (error) {
    console.error('❌ 初始化订单数据失败:', error.message);
  } finally {
    // 关闭数据库连接
    setTimeout(() => {
      mongoose.connection.close();
      console.log('数据库连接已关闭');
      process.exit(0);
    }, 1000);
  }
}, 2000); // 等待2秒确保数据库连接建立
